import {  constantRouterMap } from "@/config/router.config"


// function hasRole(roles, route) {
//     if (route.meta && route.meta.roles) {
//       return route.meta.roles.indexOf(roles.id)
//     } else {
//       return true
//     }
// }

// function hasPermission(permission, route) {
//   if (route.meta && route.meta.permission) {
//     let flag = -1
//     for (let i = 0, len = permission.length; i < len; i++) {
//       flag = route.meta.permission.indexOf(permission[i])
//       if (flag >= 0) {
//         return true
//       }
//     }
//     return false
//   }
//   return true
// }
  
// function filterAsyncRouter(routerMap, roles) {
//     const accessedRouters = routerMap.filter(route => {
//       if (hasPermission(roles.permissionList, route)) {
//         if (route.children && route.children.length) {
//           route.children = filterAsyncRouter(route.children, roles)
//         }
//         return true
//       }
//       return false
//     })
//     return accessedRouters
// }
  
const permission = {
    state: {
        routers: constantRouterMap,
        addRouters: []
    },
    mutations: {
      SET_ROUTERS: (state,data) => { 
            state.addRouters = data
            state.routers = constantRouterMap.concat(data)
        }
    },
    actions: {
        // GenerateRoutes({commit},data) { 
        //     return new Promise(resolve => { 
        //         const { roles } = data;
        //         let accessedRouters;
        //         accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        //         commit('SET_ROUTERS', accessedRouters)
        //         resolve()
        //     })
        // },
        UpdateAppRouter({ commit }, routes) {
            return new Promise(resolve => {
              let routelist = routes.constRoutes;
              commit('SET_ROUTERS', routelist)
              resolve()
            })
          }
    }
}

export default permission;